package com.etc.service.impl;

import com.etc.dao.DrugMapper;
import com.etc.entity.Drug;
import com.etc.service.DrugService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class DrugServiceImpl implements DrugService {

    private  static  final Logger logger= LoggerFactory.getLogger(DrugServiceImpl.class);

    @Autowired
    private DrugMapper drugMapper;


    @Override
    public PageInfo<Drug> getDrugDetail(String drugName, int pageNum, int pageSize) {
        if(drugName!=null&&!"".equals(drugName)){
            StringBuilder sb=new StringBuilder();
            sb.append("%").append(drugName).append("%");
            drugName=sb.toString();
        }
        logger.debug(drugName);
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Drug> drugDetail = drugMapper.findDrugDetail(drugName);
        //封装
        PageInfo<Drug> pageInfo=new PageInfo<>(drugDetail);
        return pageInfo;
    }

    @Override
    public boolean updateDrugDetail(Integer drugId, String drugName, Integer stock, BigDecimal price) {
        Drug drug=new Drug();
        drug.setDrugId(drugId);
        drug.setDrugName(drugName);
        drug.setStock(stock);
        drug.setPrice(price);
        int i = drugMapper.updateDrugDetail(drug);
        return i>0;
    }
}
